empty=
ifneq ($(ComSpec),)
RM=del
DIRSEP=\$(empty)
else
RM=rm
DIRSEP=/
endif

CASTLING_DIR=../source
BUILDDIR=build

OCAMLFLAG=-w L -warn-error A -I $(BUILDDIR)
OCAMLCFLAG=-g $(OCAMLFLAG)

CASTLING_OBJS=$(BUILDDIR)/lalr1.cmo $(BUILDDIR)/castling.cmo

tests = test01 test02 test03 test04 test05 test06 test07 test08 test09 test10 test11 test12 test13 test14 test15 test_tk test_tkpv test_pv test_pp test_calc conflict1

.PHONY: all clean $(tests) calc
.DELETE_ON_ERROR: *_parser.ml

all: $(tests)

test01: test01.ml test01_parser.ml
	ocaml $(OCAMLFLAG) $<

test01_parser.ml: test01_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test02: test02.ml test02_parser.ml
	ocaml $(OCAMLFLAG) $<

test02_parser.ml: test02_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test03: test03.ml test03_parser.ml
	ocaml $(OCAMLFLAG) $<

test03_parser.ml: test03_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test04: test04.ml test04_parser.ml
	ocaml $(OCAMLFLAG) $<

test04_parser.ml: test04_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test05: test05.ml test05_parser.ml
	ocaml $(OCAMLFLAG) $<

test05_parser.ml: test05_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test06: test06.ml test06_parser.ml
	ocaml $(OCAMLFLAG) $<

test06_parser.ml: test06_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test07: test07.ml test07_parser.ml
	ocaml $(OCAMLFLAG) $<

test07_parser.ml: test07_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test08: test08.ml test08_parser.ml
	ocaml $(OCAMLFLAG) $<

test08_parser.ml: test08_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test09: test09.ml test09_parser.ml
	ocaml $(OCAMLFLAG) $<

test09_parser.ml: test09_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test10: test10.ml test10_parser.ml
	ocaml $(OCAMLFLAG) $<

test10_parser.ml: test10_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test11: test11.ml test11_parser.ml
	ocaml $(OCAMLFLAG) $<

test11_parser.ml: test11_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test12: test12.ml test12_parser.ml
	ocaml $(OCAMLFLAG) $<

test12_parser.ml: test12_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test13: test13.ml test13_parser.ml
	ocaml $(OCAMLFLAG) $<

test13_parser.ml: test13_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test14: test14.ml test14_parser.ml
	ocaml $(OCAMLFLAG) $<

test14_parser.ml: test14_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test15: test15.ml test15_parser.ml
	ocaml $(OCAMLFLAG) $<

test15_parser.ml: test15_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test_tk: test_tk.ml test_tk_parser.ml $(BUILDDIR)/test_tk_token.cmo
	ocaml $(OCAMLFLAG) $<

test_tk_parser.ml: test_tk_syntax.ml $(CASTLING_OBJS) $(BUILDDIR)/test_tk_token.cmo
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $(BUILDDIR)/test_tk_token.cmo $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(BUILDDIR)/test_tk_token.cmo $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

$(BUILDDIR)/test_tk_token.cmo: test_tk_token.ml
	ocamlc -c -o $@ $(OCAMLCFLAG) $<

test_tkpv: test_tkpv.ml test_tkpv_parser.ml
	ocaml $(OCAMLFLAG) $<

test_tkpv_parser.ml: test_tkpv_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test_pv: test_pv.ml test_pv_parser.ml
	ocaml $(OCAMLFLAG) $<

test_pv_parser.ml: test_pv_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test_pp: test_pp.ml test_pp_parser.ml
	ocaml $(OCAMLFLAG) $(CASTLING_OBJS) $<

test_pp_parser.ml: test_pp_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

conflict1: conflict1_parser.ml

conflict1_parser.ml: conflict1_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

test_calc: test_calc.ml calc_parser.ml
	ocaml $(OCAMLFLAG) $<

calc: calc.ml calc_parser.ml
	ocaml $(OCAMLFLAG) $<

calc_parser.ml: calc_syntax.ml $(CASTLING_OBJS)
	ocamlc -c -o $(BUILDDIR)/$(basename $<).cmo $(OCAMLCFLAG) $(CASTLING_OBJS) $<
	ocamlc -o $(BUILDDIR)/$(basename $<).exe $(OCAMLCFLAG) $(CASTLING_OBJS) $(basename $<).cmo
	ocamlrun -b $(BUILDDIR)/$(basename $<).exe > $@

$(BUILDDIR)/castling.cmo: $(CASTLING_DIR)/castling.ml $(BUILDDIR)/lalr1.cmo
	ocamlc -c -o $@ $(OCAMLCFLAG) $<

$(BUILDDIR)/lalr1.cmo: $(CASTLING_DIR)/lalr1.ml
	-mkdir $(BUILDDIR)
	ocamlc -c -o $@ $(OCAMLCFLAG) $<

clean:
	-$(RM) *_parser.ml
	-$(RM) $(BUILDDIR)$(DIRSEP)*.cm? $(BUILDDIR)$(DIRSEP)*.exe
	-rmdir $(BUILDDIR)
